<?php

namespace app\admin\controller;

use app\common\model\ShopStore;
use app\common\model\SystemUser;
use buildView\Button;
use think\Controller;
use buildView\Form;
use buildView\Grid;
use app\common\model\ShopStoreStaff;
/**
 * 员工管理
 * Class Staff
 * @package app\admin\controller
 */
class Staff extends Controller
{
    /**
     * 列表
     * @auth true
     * @menu true
     * @return string
     */
    public function index()
    {
        $store = session('store');
        $grid = new Grid(new ShopStoreStaff);
        if ($store){
            $grid->model()->where('store_id',$store['id']);
        }else{
            $grid->store()->name('门店');
        }
		$grid->column('name','员工名称');
		$grid->column('phone','联系电话');
		$grid->column('address','联系地址');
        $grid->column('start_time','开始时间');
		$grid->column('end_time','结束时间');
		$grid->column('create_at','添加时间');
		$grid->filter(function ($filter)use($store){
            if (!$store) {
                $data = ShopStore::field('id,name')->column('name','id');
                $filter->eq('store_id', '门店')->select($data);
            }
            $filter->like('phone','手机号');
            $filter->dateBetween('create_at','添加时间');
        });
		$grid->setFromOpen();
        $grid->hideExportButton();
        $grid->actions(function ($action,$data){
            if ($data['admin_id'] == 0) {
                $button3 = new Button('绑定','normal');
                $button3->href(url('updateAct') . '?id=' . $data['id'], 'modal');
                $action->prepend($button3);
            }else{
                $button3 = new Button('解绑','danger');
                $button3->save($data['id'], ['admin_id' => 0,'sys_id' => $data['admin_id']],'admin/store/delectAct','是否解除绑定？');
                $action->prepend($button3);
            }
        	 $action->hideDetail();
        });
        return $grid->view();
    }

    /**
     * 添加/编辑
     * @auth true
     * @menu true
     * @return string
     */
    public function form()
    {
        $store = session('store');
        $store_id = $store?$store['id']:0;
        $form = new Form(new ShopStoreStaff);
		$form->text('name','员工名称');
		$form->text('phone','联系电话');
		$form->text('address','联系地址');
		$form->datetime('start_time','开始时间');
		$form->datetime('end_time','结束时间');
        $form->hidden('store_id','店铺')->default($store_id);
        return $form->view();
    }

    /**
     * 绑定用户
     * @auth true
     * @menu true
     */
    public function updateAct()
    {
        $store = session('store');
        $store_id = $store?$store['id']:0;
        $form = new Form(new ShopStoreStaff());
        $user = SystemUser::where('is_open',0)->where('store_id',$store_id)->select()->column('search_tip','id');
        $form->select('admin_id','员工账号')->options($user)->required();
        $form->saving(function ($post) {
            SystemUser::where('id',$post['admin_id'])->setField('is_open',1);
            return $post;
        });
        return $form->view();
    }

    /**
     * 解绑用户
     * @auth true
     * @menu true
     */
    public function delectAct()
    {
        $id = $this->request->param('id');
        $admin_id = $this->request->param('sys_id');
        ShopStoreStaff::where('id',$id)->setField('admin_id',0);
        SystemUser::where('id',$admin_id)->setField('is_open',0);
        $this->success('成功');
    }
}
